<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: showmatrix.php 1126 2008-06-09 05:45:04Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 


  /**
   * Returns rows of games info for matrix display
   *
   * @param Joomleague $project
   * @param int $division
   * @param string $unpublished
   * @return array rows objects
   */
  function getMatrixResults( $project_id, $division = 0, $unpublished = 0 ) {
  	global $database;  
    
    $query_SELECT = "SELECT r.matchcode,
                            m.match_id,
                            m.show_report,
                            m.matchpart1,
                            m.matchpart2,
                            m.matchpart1_result as e1,
                            m.matchpart2_result as e2,
                            m.alt_decision as decision,
                            m.matchpart1_result_decision AS v1,
                            m.matchpart2_result_decision AS v2
                            ";
    $query_FROM  = " FROM #__joomleague_matches AS m
                     INNER JOIN #__joomleague_rounds AS r ON r.id=m.round_id";
    $query_WHERE = " WHERE  m.project_id = ".$project_id;
    $query_END   = "";
      
    if ($division > 0) {
      $query_FROM  .= " INNER JOIN #__joomleague_team_joomleague AS tt1 ON m.matchpart1 = tt1.team_id
                        INNER JOIN #__joomleague_team_joomleague AS tt2 ON m.matchpart2 = tt2.team_id
                        LEFT JOIN #__joomleague_divisions AS d1 ON tt1.division_id = d1.id
                        LEFT JOIN #__joomleague_divisions AS d2 ON tt2.division_id = d2.id";
      $query_WHERE .= " AND (  d1.id = $division OR d1.parent_id = $division  
                            OR d2.id = $division OR d2.parent_id = $division )";
    }
      
    if ($unpublished!=1) $query_WHERE .=" AND m.published = 1";

    $database->setQuery( $query_SELECT.$query_FROM.$query_WHERE.$query_END );
    if ( !$result = $database->loadObjectList() ) {
      echo $database->getErrorMsg();
    }
    return $result;
  }
  
/**
 * displays the results matrix for the project
 *
 */  
function showmatrix() 
{
	global $my, $database, $mainframe;
	global $joomleague, $overall_config;
    global $htmlHelper;

	if ($joomleague->project_type == 'SIMPLE_KO') {
		$url = sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showResults&amp;p='.$joomleague->id.JL_ITEMID_SUFFIX);
		mosRedirect($url);
	}
	$showMatrix_config=$joomleague->getTemplateConfig("show_matrix.tpl");
	$mainframe->setPageTitle(_JL_PAGE_TITLE_SHOW_MATRIX);
	
	$divisionid = intval(mosGetParam($_REQUEST, 'division', 0));
	if ($divisionid > 0) {
		$division = new LeagueDivision($database);
		$division->load($divisionid);
	}
	// get project teams info, restricted to teams division if not null
	$teams = $joomleague->getTeamsIndexedById(null, $divisionid);
	// get games
	$results = getMatrixResults($joomleague->id, $divisionid);
	 
	require(getTemplateFile(JL_TEMPLATE_PATH, 'show_matrix.tpl.php'));
}

?>